﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;

namespace QuanLyKhachSan
{
    public class TabManagers
    {
        public static XtraTabControl tabParent;
        public static void AddDynamicTab<T>() where T : XtraUserControl, new()
        {
            XtraTabPage isActive = Find<T>();
            if (isActive == null)
            {
                XtraUserControl newUserControl = new T();
                var tabPage = new XtraTabPage();
                tabPage.Controls.Add(newUserControl);
                tabPage.Text = ((IGeneatetabName) newUserControl).Getname();
                newUserControl.Dock = System.Windows.Forms.DockStyle.Fill;
                tabParent.TabPages.Add(tabPage);
                tabParent.SelectedTabPage = tabPage;
            }
            else
                tabParent.SelectedTabPage = isActive;

        }

        private static XtraTabPage Find<T>() where T : XtraUserControl
        {
            foreach (XtraTabPage tabPage in tabParent.TabPages)
            {
                if (tabPage.Controls.OfType<T>().Any())
                {
                    return tabPage;
                }
            }
            return null;
        }

        public static void CloseDynamicTab(XtraTabPage tabPage)
        {
            tabParent.TabPages.Remove(tabPage);
        }
    }
}
